package com.icepeach.icepeachhomeback.controller;

import cn.hutool.core.util.StrUtil;
import com.icepeach.icepeachhomeback.entity.Category;
import com.icepeach.icepeachhomeback.req.CategoryListQueryReq;
import com.icepeach.icepeachhomeback.service.CategoryService;
import com.icepeach.icepeachhomeback.util.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Resource
    CategoryService categoryService;
    /**
     * 获取所有分类
     *
     * @return
     */
    @PostMapping("/getAllCategory")
    @ResponseBody
    public ApiResult getAllCategory() {
        List<Category> list = categoryService.list(null);
        return ApiResult.success("查询成功", list);
    }
    /**
     * 根据id获取单个文章
     *
     * @return
     */
    @PostMapping("/getCategoryById")
    @ResponseBody
    public ApiResult getCategoryById(String id) {
if(StrUtil.isNotBlank(id)){
    return ApiResult.success("查询成功", categoryService.getById(id));
}else {
    return ApiResult.failure("请求异常");
}
    }


    /**
     * 分页查询 获取分类
     *
     * @param categoryListQueryReq
     * @return
     */
    @PostMapping("/getCategorysByPage")
    @ResponseBody
    public ApiResult getCategorysByPage(@RequestBody CategoryListQueryReq categoryListQueryReq) {

        return ApiResult.success("查询成功", categoryService.getCategorysByPage(categoryListQueryReq));
    }

    /**
     * 增加或修改分类
     * @param category
     * @return
     */

    @PostMapping("/saveCategory")
    @ResponseBody
    public ApiResult saveCategory(@RequestBody Category category) {
        boolean result = categoryService.saveOrUpdate(category);
        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public ApiResult deleteById(@RequestBody Category category) {
        boolean result = categoryService.removeById(category.getId());

        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }
    @PostMapping("/del/batch")
    @ResponseBody
    public ApiResult deleteBatch(@RequestBody List<String> ids) {
        categoryService.removeByIds(ids);
        return ApiResult.success("成功");
    }
}
